﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class reciboAdelantado : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        buscar1.Attributes.Add("OnClick", "javascript:popUpClientes();");
        fecha2.Text = System.DateTime.Now.ToString();

    }
    protected void buscar1_Click(object sender, ImageClickEventArgs e)
    {

        String cadena = (String)ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conexion = new SqlConnection(cadena);
        try
        {
            conexion.Open();
            SqlCommand numero = new SqlCommand();
            numero.CommandText = "select COUNT(cod_recibo_pago_adelantado) as cantidad from recibo_pago_adelantado where cod_cliente =" + nombreTex.Text;
            numero.Connection = conexion;
            numero.CommandType = CommandType.Text;

            SqlDataReader numero_reader = numero.ExecuteReader();
            numero_reader.Read();
            totalesMonto.Text = numero_reader.GetValue(0).ToString();
            numero_reader.Close();

            SqlDataSource1.SelectParameters.Clear();
            SqlDataSource1.SelectCommand = "SELECT monto, fecha, cod_cliente FROM recibo_pago_adelantado WHERE cod_cliente = @cod_cliente";
            SqlDataSource1.SelectParameters.Add("cod_cliente", TypeCode.Int32, nombreTex.Text);
            GridRecibo.DataBind();
        }
        catch (Exception error)
        {
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta(" + error.Message.ToString() + ") </SCRIPT>");
            
        }
        finally
        {
            conexion.Close();
        }
        


    }


    protected void guardar_Click(object sender, ImageClickEventArgs e)
    {
        nombreTex.Text = null;
        montoTex.Text = null;
        LError.Text = null;
        totalesMonto.Text = "0";

        GridRecibo.Columns.Clear();
        GridRecibo.DataBind();
        Page.RegisterStartupScript("alerta", "<SCRIPT> alerta('Datos Correctamente Guardados') </SCRIPT>");
    }
   

    protected void borrar_Click(object sender, ImageClickEventArgs e)
    {
        nombreTex.Text = null;
        montoTex.Text = null;
        LError.Text = null;
        totalesMonto.Text = "0";

        GridRecibo.Columns.Clear();
        GridRecibo.DataBind();
        Page.RegisterStartupScript("alerta", "<SCRIPT> alerta('Datos Cancelados') </SCRIPT>");
    }
    protected void AgregarProducto_Click(object sender, ImageClickEventArgs e)
    {
        String cadena = (String)ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conexion = new SqlConnection(cadena);
        try
        {
            conexion.Open();
            SqlCommand comando = new SqlCommand();
            comando.Connection = conexion;
            comando.CommandType = System.Data.CommandType.StoredProcedure;

            
            

            if (nombreTex.Text == "" || montoTex.Text == "")
            {
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta('Los campos con * se deben completar') </SCRIPT>");
            }
            else
            {
                SqlCommand select = new SqlCommand();
                select.CommandText = "Select cod_cliente from cliente_cc where cod_cliente="+nombreTex.Text;
                select.Connection = conexion;
                select.CommandType = CommandType.Text;
                SqlDataReader cliente_reader = select.ExecuteReader();
                cliente_reader.Read();
                Clitex.Text = cliente_reader.GetValue(0).ToString();
                cliente_reader.Close();
               
                
                if(Clitex.Text == null){

                    Page.RegisterStartupScript("alerta", "<SCRIPT> alerta('El cliente no posee una Cuenta corriente') </SCRIPT>");
                
                }
                else{
                comando.CommandText = "insertar_recibo_adelantado";
                comando.Parameters.AddWithValue("@monto", montoTex.Text);
                comando.Parameters.AddWithValue("@cod_cliente", nombreTex.Text);
                comando.Parameters.AddWithValue("@cod_funcionario", "1");

                SqlDataReader reader = comando.ExecuteReader();
                LError.Text = "Datos guardados";

                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "SELECT monto, fecha, cod_cliente FROM recibo_pago_adelantado WHERE cod_cliente = @cod_cliente";
                SqlDataSource1.SelectParameters.Add("cod_cliente", TypeCode.Int32, nombreTex.Text);
                GridRecibo.DataBind();

                reader.Close();

                SqlCommand numero = new SqlCommand();
                numero.CommandText = "select COUNT(cod_recibo_pago_adelantado) as cantidad from recibo_pago_adelantado where cod_cliente =" + nombreTex.Text;
                numero.Connection = conexion;
                numero.CommandType = CommandType.Text;

                SqlDataReader numero_reader = numero.ExecuteReader();
                numero_reader.Read();
                totalesMonto.Text = numero_reader.GetValue(0).ToString();
                numero_reader.Close();
                }

            }
        }
        catch (Exception error)
        {
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta(" + error.Message.ToString() + ") </SCRIPT>");
            LError.Text = "no guardado ,,catch";
        }
        finally
        {
            conexion.Close();

        }
    }
}
